package com.leostadyn.stackfarm.timestatistics.view.activity.employee;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;

import com.leostadyn.stackfarm.timestatistics.R;
import com.leostadyn.stackfarm.timestatistics.base.BaseActivity;
import com.leostadyn.stackfarm.timestatistics.net.api.AdminApi;
import com.leostadyn.stackfarm.timestatistics.net.base.OriginalSubscriber;
import com.leostadyn.stackfarm.timestatistics.net.base.ResponseThrowable;
import com.leostadyn.stackfarm.timestatistics.utils.DateUtils;
import com.leostadyn.stackfarm.timestatistics.utils.ToastUtil;

import java.util.Calendar;
import java.util.Date;

public class EmployeeAddActivity extends BaseActivity implements View.OnClickListener {
    TextView dateTxt;
    Button commitBtn;
    TextView projectTxt;
    String projectId;
    EditText dayEdit;
    String startString;
    String endString;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_employee_add_edit);
        initView();
    }


    void initView() {
        TextView titleTxt = findViewById(R.id.nav_txt_title);
        titleTxt.setText("添加工时");
        findViewById(R.id.nav_img_back).setOnClickListener(v -> onBackPressed());
        findViewById(R.id.nav_btn_add).setVisibility(View.GONE);

        findViewById(R.id.layout_date).setOnClickListener(this);
        dateTxt = findViewById(R.id.txt_start);
        commitBtn = findViewById(R.id.btn_commit);
        projectTxt = findViewById(R.id.txt_employee_project);
        dayEdit = findViewById(R.id.edit_employee_consume);

        commitBtn.setOnClickListener(this);
        projectTxt.setOnClickListener(this);

        Intent intent = getIntent();
        startString = intent.getStringExtra("startString");
        endString = intent.getStringExtra("endString");
        dateTxt.setText(intent.getStringExtra("date"));
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        switch (requestCode) {
            case 1:
                if (resultCode == Activity.RESULT_OK) {
                    if (data != null) {
                        projectTxt.setText(data.getStringExtra("code") + ":" + data.getStringExtra("name"));
                        projectId = data.getStringExtra("id");
                    }
                }
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.txt_employee_project:
                Intent intent = new Intent(EmployeeAddActivity.this, ProjectSelectActivity.class);
                intent.putExtra("mIndex", "-1");
                startActivityForResult(intent, 1);
                break;

            case R.id.layout_date:

                Date date = DateUtils.getDate2(startString);
                Calendar calendar = Calendar.getInstance();//日历对象
                calendar.setTime(date);
                DatePickerDialog dialogStart = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        String str = String.format("%04d.%02d.%02d", year, month + 1, dayOfMonth);
                        dateTxt.setText(str);
                    }
                },
                        calendar.get(Calendar.YEAR),
                        calendar.get(Calendar.MONTH),
                        calendar.get(Calendar.DATE)
                );
                dialogStart.show();
                break;

            case R.id.btn_commit:
                String consume = dayEdit.getText().toString();
                String day = dateTxt.getText().toString();
                if (!day.equals("")) {
                    if (day.compareTo(startString) >= 0 && day.compareTo(endString) <= 0) {
                        if (consume.length() <= 4) {
                            if (projectId != null) {
                                if (!consume.equals("")) {
                                    if (Float.parseFloat(consume) != 0.5 && Float.parseFloat(consume) != 1) {
                                        ToastUtil.show(this, "请输入0.5或1.0");
                                    } else {
                                        commitTimeRecord(projectId, Float.parseFloat(consume), day);
                                    }
                                } else {
                                    ToastUtil.show(this, "请填写完整耗时");
                                }
                            } else {
                                ToastUtil.show(this, "请选择项目");
                            }
                        } else {
                            ToastUtil.show(this, "耗时最低两位小数");
                        }
                    } else {
                        ToastUtil.show(this, "所选日期不在该工作周内");
                    }
                } else {
                    ToastUtil.show(this, "请填写日期");
                }
                break;

            default:
                break;
        }
    }

    void commitTimeRecord(String projectId, float taskTime, String workDate) {
        AdminApi.getInstance().addTimeRecord(projectId, taskTime, workDate).subscribe(new OriginalSubscriber<String>(compositeDisposable) {
            @Override
            public void onError(ResponseThrowable e) {
                ToastUtil.show(EmployeeAddActivity.this, e.getMsg());
            }

            @Override
            public void onNext(String string, String msg) {
                ToastUtil.show(EmployeeAddActivity.this, "工时添加成功");
                finish();
            }
        });
    }
}
